package com.owen.client.ui.widget;

import java.util.HashSet;
import java.util.Set;

import com.google.gwt.cell.client.Cell.Context;
import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.ImageCell;
import com.google.gwt.cell.client.ImageResourceCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.owen.client.UIFactoryInstance;
import com.owen.client.ui.ClassPresenter.Display;
import com.owen.liferay.shared.ClassProxy;
import com.owen.shared.events.DeleteClassEvent;
import com.owen.shared.events.EditClassEvent;

public class ClassWidget extends Composite implements Display {

	private static ClassWidgetUiBinder uiBinder = GWT
			.create(ClassWidgetUiBinder.class);

	interface ClassWidgetUiBinder extends UiBinder<Widget, ClassWidget> {
	}
	
	// this is for the delete icon/image
	interface Resources extends ClientBundle {
		  @Source("../images/delete_icon.gif")
		  ImageResource getImageResource();
	}
	
	Resources resources = GWT.create(Resources.class);

	@UiField(provided=true) 
	final CellTable<ClassProxy> table = new CellTable<ClassProxy>(); 
	
	@UiField(provided=true)
	final ClassEditor editor = new ClassEditor();
	
	@UiField
	Button addNew;
	
	public ClassWidget() {
		initWidget(uiBinder.createAndBindUi(this));
	    
	    class AuthCol extends Column<ClassProxy, String> {
			public AuthCol() {
				super(new ClickableTextCell());
			}

			@Override
			public String getValue(ClassProxy clazz) {
				return clazz.getClassId();
			}

			@Override
			public void onBrowserEvent(Context context, Element elem,
					ClassProxy clazz, NativeEvent event) {
				super.onBrowserEvent(context, elem, clazz, event);
				
				UIFactoryInstance.getInstance().getEventBus().fireEvent(new EditClassEvent(clazz));
			}
			
			
	    }
	    
	    class PubCol extends Column<ClassProxy, String> {
			public PubCol() {
				super(new ClickableTextCell());
			}

			@Override
			public String getValue(ClassProxy clazz) {
				return clazz.getClassName();
			}

			@Override
			public void onBrowserEvent(Context context, Element elem,
					ClassProxy clazz, NativeEvent event) {
				super.onBrowserEvent(context, elem, clazz, event);
				
				UIFactoryInstance.getInstance().getEventBus().fireEvent(new EditClassEvent(clazz));
			}
			
	    }	    
      
	    // Create delete icon column
	    class DeleteColumn extends Column<ClassProxy, String> {
			public DeleteColumn() {
				super(new ClickableTextCell());
			}
			
			
			@Override
			public void onBrowserEvent(Context context, Element elem, ClassProxy clazz, NativeEvent event) {
				super.onBrowserEvent(context, elem, clazz, event);

				UIFactoryInstance.getInstance().getEventBus().fireEvent(new DeleteClassEvent(clazz.getId()));
	         
			}


			@Override
			public void render(Context context, ClassProxy object,
					SafeHtmlBuilder sb) {
				super.render(context, object, sb);

				if (object == null) {
					return;
				}

				sb.appendHtmlConstant("<div style=\"text-decoration: underline;\">");
				sb.appendHtmlConstant("<img src='images/delete_icon.gif'/>");
				sb.appendHtmlConstant("</div>");
			}

			@Override
			public String getValue(ClassProxy object) {
				return null;
			}
		}
	  
	    // Add the columns.
	    table.addColumn(new AuthCol(), "Class Id");
	    table.addColumn(new PubCol(), "Class Name");
	    table.addColumn(new DeleteColumn(), "");
	}

	@Override
	public CellTable getTable() {
		return table;
	}

	@Override
	public ClassEditor getEditor() {
		return editor;
	}

	@Override
	public Button getAddNew() {
		return addNew;
	}
	

}
